//
//  CommentTableViewCell.m
//  Arts1
//
//  Created by Mac Jc on 15/10/4.
//  Copyright © 2015年 Arts1. All rights reserved.
//

#import "CommentTableViewCell.h"
#import "AllHeader.h"


@implementation CommentTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH, 0.5)];
        lineView.backgroundColor = kCustomColor(150, 150, 150, 0.5);
        [self.contentView addSubview:lineView];
        
        _iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, 10, 30, 30)];
        _iconImage.layer.cornerRadius = 15;
        _iconImage.layer.masksToBounds = YES;
        _iconImage.backgroundColor = kRandomColor;
        [_iconImage setContentMode:(UIViewContentModeScaleAspectFill)];
        [self.contentView addSubview:_iconImage];
        
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(_iconImage.right + 10, _iconImage.top + 3, 100, 15)];
        _nameLabel.font = [UIFont boldSystemFontOfSize:11];
        _nameLabel.textColor = kCustomColor(40, 40, 50, 1);
        [self.contentView addSubview:_nameLabel];
        
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 120, _nameLabel.top, 100, 10)];
        _timeLabel.font = [UIFont systemFontOfSize:10];
        [_timeLabel setTextAlignment:(NSTextAlignmentRight)];
        _timeLabel.textColor = kCustomColor(150, 150, 150, 1);
        [self.contentView addSubview:_timeLabel];

        _commentContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(_nameLabel.left, _nameLabel.bottom, SCREEN_WIDTH - _iconImage.right + 20, 13)];
        _commentContentLabel.font = [UIFont systemFontOfSize:12];
        [_commentContentLabel setNumberOfLines:0];
        _commentContentLabel.lineBreakMode = NSLineBreakByWordWrapping;
        _commentContentLabel.textColor = kCustomColor(150, 150, 150, 1);
        [self.contentView addSubview:_commentContentLabel];
   
    }
    return  self;
}

- (void)setModel:(HomeCommentModel *)model
{
    
    _model = model;
    [ _iconImage sd_setImageWithURL:[NSURL URLWithString:_model.userFace] placeholderImage:nil options:SDWebImageRetryFailed | SDWebImageLowPriority ];
    _nameLabel.text = _model.nickName;
    
    
    
    //显示footer上面的数据,由于获取的数据是乘了1000倍，所有要除以1000，而86400刚好是一天时间的秒数，意思就是减去一天的时间
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[_model.addTime intValue] - 86400];
    //格式化时间
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    //设置格式
    [formater setDateFormat:@"MM月dd日 HH:mm"];
    //获取系统的时间区域
    formater.timeZone = [NSTimeZone systemTimeZone];
    //设定本地化，时间会按照本地的习惯和文字显示日期
    formater.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-us"];
    NSString *string = [formater stringFromDate:date];
    
    
    _timeLabel.text = string;
    
    CGFloat height = [CommentTableViewCell heightOfString:_model.comment];
    _commentContentLabel.frame = CGRectMake(55, 30, 280, height);
    
    _commentContentLabel.text = _model.comment;
}

+ (CGFloat)heightOfString:(NSString *)str
{
    CGRect bounds = [str boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 50, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName] context:nil];
    return bounds.size.height;
}

+ (CGFloat)heightOfCell:(HomeCommentModel *)model
{
    CGFloat height1 = [CommentTableViewCell heightOfString:model.comment];

    return 50 + height1 ;
}

@end
